package cn.com.utt.common.util;

import java.security.MessageDigest;

public class Sha256Encode {

	public static String encode(String code) {
		MessageDigest md;
		String encode = null;
		try {
			md = MessageDigest.getInstance("SHA-256");
			md.update(code.getBytes("UTF-8")); // Change this to "UTF-16" if
			encode = bytes2Hex(md.digest()); // to HexString // needed
		} catch (Exception e) {

		}

		return encode;
	}

	public static String bytes2Hex(byte[] bts) {
		String des = "";
		String tmp = null;
		for (int i = 0; i < bts.length; i++) {
			tmp = (Integer.toHexString(bts[i] & 0xFF));
			if (tmp.length() == 1) {
				des += "0";
			}
			des += tmp;
		}
		return des;
	}
}
